package com.student.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.student.model.Colleges;
import com.student.model.Tuser;
import com.student.service.LoginService;
import com.student.util.UserRoles;

/**
 * @author 陈伟斌 用户登录action
 */
@Component("LoginAction")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class LoginAction extends SuperAction {

	private String userName;// 用户名
	private String password;// 密码
	private String department;// 登陆的部门
	private String validateCode;// 验证码
	private List<Colleges> colleges;// 所有学院列表
	private LoginService loginService;
	private Long collegeId = null;// 二级学院的id
	private String result;
	private boolean isAdmin;// 标识是否为后台管理员

	public boolean isAdmin() {
		return isAdmin;
	}

	public void setAdmin(boolean isAdmin) {
		this.isAdmin = isAdmin;
	}

	@Override
	public String execute() throws Exception {
		colleges = loginService.getCollegesList();
		return SUCCESS;
	}

	public String loginValidate() throws Exception {
		
		try {
			String userRole = null;
			String realalidateCode = (String)ActionContext.getContext().getSession().get("ValidateCode");// 正确的验证码
			
			if (realalidateCode==null||!realalidateCode.equals(validateCode)) {
				result = "验证码错误！";
				return SUCCESS;
			}
			if(department==null){
				userRole=null;
			}else if (department.equals("leader")) {// 领导
				userRole = UserRoles.LEADER;
			} else if (department.equals("finance")) {
				userRole = UserRoles.FINANCE;// 财务处
			} else if (department.equals("enrollment")) {// 招就处
				userRole = UserRoles.ENROLLMENT;
			} else if (department.equals("student")) {// 学生处
				userRole = UserRoles.STUDENT;
			} else if (department.equals("logistics")) {// 后勤部
				userRole = UserRoles.LOGISTICS;
			} else if (department.equals("declare")) {// 宣保处
				userRole = UserRoles.DECLARE;
			}else if (department.equals("colleges")) {// 二级学院
				if (isAdmin) {
					userRole = UserRoles.ADMIN;// 二级学院后台管理员
				} else {
					userRole = UserRoles.FORE_ADMIN;// 二级学院前台管理员
				}
			} else {
				result = "登陆失败";
				this.logSuccessString("loginValidate",  "验证登陆",result);
				return SUCCESS;
			}
			Tuser user = loginService.validateLoginUser(userName, password,
					userRole, collegeId);
			if (user != null) {

				result = "success";
				ActionContext.getContext().getSession().put("user", user);// 存入session中
				if (department!=null&&department.equals("colleges")) {// 如果是二级学院，则将学院对象存入session中

					if (collegeId != null) {
						ActionContext.getContext().getSession().put("college",
								loginService.getcCollegeById(collegeId));
						//isAdmin用于标识是否显示二级学院的宿舍管理和财务管理（只有超级管理员或后台管理员才有此权限）

						ActionContext.getContext().getSession().put("isAdmin",isAdmin||user.getUserType().getTypeName().equals(UserRoles.SUPER_ADMIN)||user.getUserType().getTypeName().equals(UserRoles.SPECIAL_USER));
					}
				}else{
					ActionContext.getContext().getSession().remove("college");
					
				}
				ActionContext.getContext().getSession().put("isSpecialUser",user.getUserType().getTypeName().equals(UserRoles.SPECIAL_USER));

				this.logSuccessString("loginValidate",  "验证登陆",result);
			} else {
				result = "用户名或密码错误";
			}
			
			return SUCCESS;
		} catch (Exception e) {
			this.logFailureString("loginValidate", "验证登陆", e);
			e.printStackTrace();
			throw new Exception(e);
		}

	}
	//退出
	public String logout() throws Exception{
		try {
			this.logSuccessString("logout",  "退出登陆","退出");
			ActionContext.getContext().getSession().clear();//清除session 中的信息
			colleges = loginService.getCollegesList();		
			return LOGIN;
		} catch (Exception e) {
			this.logFailureString("logout",  "退出登陆", e);
			e.printStackTrace();
			throw new Exception(e);
		}
	}
	public String getResult() {
		return result;
	}

	public List<Colleges> getColleges() {
		return colleges;
	}

	public void setColleges(List<Colleges> colleges) {
		this.colleges = colleges;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setCollegeId(Long collegeId) {
		this.collegeId = collegeId;
	}

	public void setResult(String result) {
		this.result = result;
	}

	@Resource(name = "LoginService")
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}
}
